package cn.webestar.scms.service;

import cn.webestar.scms.commons.R;
import cn.webestar.scms.security.SecurityCorp;
import cn.webestar.scms.security.SecurityService;
import cn.webestar.scms.security.SecurityUser;
import cn.webestar.scms.security.api.AccountLoginDto;
import cn.webestar.scms.security.api.PhoneNumLoginDto;
import cn.webestar.scms.user.api.EmployeeApi;
import cn.webestar.scms.user.api.RoleApi;
import cn.webestar.scms.user.api.SysResourceApi;
import cn.webestar.scms.user.api.UserApi;
import cn.webestar.scms.user.api.dto.Employee.EmployeeSimpleDTO;
import cn.webestar.scms.user.api.dto.User.UserSimpleDTO;
import cn.webestar.scms.user.entity.Employee;
import cn.webestar.scms.user.entity.Role;
import cn.webestar.scms.user.entity.SysResource;
import cn.webestar.scms.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class SimpleSecurityService implements SecurityService {

    @Autowired
    private UserApi userApi;

    @Autowired
    private EmployeeApi employeeApi;

    @Autowired
    private RoleApi roleApi;

    @Autowired
    private SysResourceApi sysResourceApi;

    @Override
    public SecurityUser loadSecurityUser(AccountLoginDto dto) {
        return null;
//        //查找用户
//        UserSimpleDTO param = new UserSimpleDTO();
//        param.setUsername(dto.getUsername());
//        R<User> r = userApi.findOne(param);
//        User user = r.getData();
//
//        //查找员工
//        EmployeeSimpleDTO employeeSimpleDTO = new EmployeeSimpleDTO();
//        employeeSimpleDTO.setCid(dto.getCid());
//        employeeSimpleDTO.setUserId(user.getId());
//        employeeSimpleDTO.setIsDeleted(false);
//        R<Employee> empR = employeeApi.findOne(employeeSimpleDTO);
//        Employee emp = empR.getData();
//
//        R<List<Role>> rolsR = roleApi.findByEmpId(emp.getId());
//        Set<Long> sysResourceIds = new HashSet<>();
//        rolsR.getData().forEach(i -> sysResourceIds.addAll(i.getResourceIds()));
//
//        R<Map<Long, SysResource>> sysResourcesR = sysResourceApi.findByIds(sysResourceIds);
//        Set<String> permissions = new HashSet<>();
//        sysResourcesR.getData().forEach((k,v) -> permissions.add(v.getCode()));
//
//        SecurityUser securityUser = new SecurityUser();
//        securityUser.setCid(dto.getCid());
//        securityUser.setUserId(user.getId());
//        securityUser.setUsername(user.getUsername());
//        securityUser.setPassword(user.getPwd());
//        securityUser.setPhoneNum(user.getPhoneNum());
//        securityUser.setAvatarUrl(user.getAvatarUrl());
//        securityUser.setEmpId(emp.getId());
//        securityUser.setName(emp.getName());
//        securityUser.setPermissions(permissions);
//
//        return securityUser;
    }

    @Override
    public SecurityUser loadSecurityUser(PhoneNumLoginDto dto) {
        return null;
    }

    @Override
    public Object sendSmsCode(Integer type, String phoneNum, String smsCode) {
        return null;
    }

    @Override
    public List<SecurityCorp> loadUserCorps(Long userId) {
        return List.of();
    }

    @Override
    public Boolean switchCorp(Long id) {
        return null;
    }

}
